home *** CD-ROM | disk | FTP | other *** search
/ Programming Languages Suite / ProgramD2.iso / Borland / Borland C++ V5.02 / SCRIB4.PAK / SCRIBVW.CPP < prev    next >
C/C++ Source or Header  |  1997-05-06  |  8KB  |  261 lines

  1. // ScribVw.cpp : implementation of the CScribbleView class
  2. //
  3. // This is a part of the Microsoft Foundation Classes C++ library.
  4. // Copyright (C) 1992-1995 Microsoft Corporation
  5. // All rights reserved.
  6. //
  7. // This source code is only intended as a supplement to the
  8. // Microsoft Foundation Classes Reference and related
  9. // electronic documentation provided with the library.
  10. // See these sources for detailed information regarding the
  11. // Microsoft Foundation Classes product.
  12.  
  13. #include "stdafx.h"
  14. #include "Scribble.h"
  15.  
  16. #include "ScribDoc.h"
  17. #include "ScribVw.h"
  18.  
  19. #ifdef _DEBUG
  20. #define new DEBUG_NEW
  21. #undef THIS_FILE
  22. static char THIS_FILE[] = __FILE__;
  23. #endif
  24.  
  25. /////////////////////////////////////////////////////////////////////////////
  26. // CScribbleView
  27.  
  28. IMPLEMENT_DYNCREATE(CScribbleView, CScrollView)
  29.  
  30. BEGIN_MESSAGE_MAP(CScribbleView, CScrollView)
  31.     //{{AFX_MSG_MAP(CScribbleView)
  32.     ON_WM_LBUTTONDOWN()
  33.     ON_WM_LBUTTONUP()
  34.     ON_WM_MOUSEMOVE()
  35.     //}}AFX_MSG_MAP
  36.     // Standard printing commands
  37.     ON_COMMAND(ID_FILE_PRINT, CView::OnFilePrint)
  38.     ON_COMMAND(ID_FILE_PRINT_DIRECT, CView::OnFilePrint)
  39.     ON_COMMAND(ID_FILE_PRINT_PREVIEW, CView::OnFilePrintPreview)
  40. END_MESSAGE_MAP()
  41.  
  42. /////////////////////////////////////////////////////////////////////////////
  43. // CScribbleView construction/destruction
  44.  
  45. CScribbleView::CScribbleView()
  46. {
  47.     // TODO: add construction code here
  48.  
  49. }
  50.  
  51. CScribbleView::~CScribbleView()
  52. {
  53. }
  54.  
  55. BOOL CScribbleView::PreCreateWindow(CREATESTRUCT& cs)
  56. {
  57.     // TODO: Modify the Window class or styles here by modifying
  58.     //  the CREATESTRUCT cs
  59.  
  60.     return CView::PreCreateWindow(cs);
  61. }
  62.  
  63. /////////////////////////////////////////////////////////////////////////////
  64. // CScribbleView drawing
  65.  
  66. void CScribbleView::OnDraw(CDC* pDC)
  67. {
  68.     CScribbleDoc* pDoc = GetDocument();
  69.     ASSERT_VALID(pDoc);
  70.  
  71.     // Get the invalidated rectangle of the view, or in the case
  72.     // of printing, the clipping region of the printer dc.
  73.     CRect rectClip;
  74.     CRect rectStroke;
  75.     pDC->GetClipBox(&rectClip);
  76.  
  77.     // Note: CScrollView::OnPaint() will have already adjusted the
  78.     // viewport origin before calling OnDraw(), to reflect the
  79.     // currently scrolled position.
  80.  
  81.     // The view delegates the drawing of individual strokes to
  82.     // CStroke::DrawStroke().
  83.     CTypedPtrList<CObList,CStroke*>& strokeList = pDoc->m_strokeList;
  84.     POSITION pos = strokeList.GetHeadPosition();
  85.     while (pos != NULL)
  86.     {
  87.         CStroke* pStroke = strokeList.GetNext(pos);
  88.         rectStroke = pStroke->GetBoundingRect();
  89.         if (!rectStroke.IntersectRect(&rectStroke, &rectClip))
  90.             continue;
  91.         pStroke->DrawStroke(pDC);
  92.     }
  93. }
  94.  
  95. /////////////////////////////////////////////////////////////////////////////
  96. // CScribbleView printing
  97.  
  98. BOOL CScribbleView::OnPreparePrinting(CPrintInfo* pInfo)
  99. {
  100.     // default preparation
  101.     return DoPreparePrinting(pInfo);
  102. }
  103.  
  104. void CScribbleView::OnBeginPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
  105. {
  106.     // TODO: add extra initialization before printing
  107. }
  108.  
  109. void CScribbleView::OnEndPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
  110. {
  111.     // TODO: add cleanup after printing
  112. }
  113.  
  114. /////////////////////////////////////////////////////////////////////////////
  115. // CScribbleView diagnostics
  116.  
  117. #ifdef _DEBUG
  118. void CScribbleView::AssertValid() const
  119. {
  120.     CScrollView::AssertValid();
  121. }
  122.  
  123. void CScribbleView::Dump(CDumpContext& dc) const
  124. {
  125.     CScrollView::Dump(dc);
  126. }
  127.  
  128. CScribbleDoc* CScribbleView::GetDocument() // non-debug version is inline
  129. {
  130.     ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CScribbleDoc)));
  131.     return (CScribbleDoc*)m_pDocument;
  132. }
  133. #endif //_DEBUG
  134.  
  135. /////////////////////////////////////////////////////////////////////////////
  136. // CScribbleView message handlers
  137.  
  138. void CScribbleView::OnLButtonDown(UINT, CPoint point) 
  139. {
  140.     // Pressing the mouse button in the view window starts a new stroke
  141.  
  142.     // CScrollView changes the viewport origin and mapping mode.
  143.     // It's necessary to convert the point from device coordinates
  144.     // to logical coordinates, such as are stored in the document.
  145.     CClientDC dc(this);
  146.     OnPrepareDC(&dc);
  147.     dc.DPtoLP(&point);
  148.  
  149.     m_pStrokeCur = GetDocument()->NewStroke();
  150.     // Add first point to the new stroke
  151.     m_pStrokeCur->m_pointArray.Add(point);
  152.  
  153.     SetCapture();       // Capture the mouse until button up.
  154.     m_ptPrev = point;   // Serves as the MoveTo() anchor point for the
  155.                         // LineTo() the next point, as the user drags the
  156.                         // mouse.
  157.  
  158.     return;
  159. }
  160.  
  161. void CScribbleView::OnLButtonUp(UINT, CPoint point) 
  162. {
  163.     // Mouse button up is interesting in the Scribble application
  164.     // only if the user is currently drawing a new stroke by dragging
  165.     // the captured mouse.
  166.  
  167.     if (GetCapture() != this)
  168.         return; // If this window (view) didn't capture the mouse,
  169.                 // then the user isn't drawing in this window.
  170.  
  171.     CScribbleDoc* pDoc = GetDocument();
  172.  
  173.     CClientDC dc(this);
  174.  
  175.     // CScrollView changes the viewport origin and mapping mode.
  176.     // It's necessary to convert the point from device coordinates
  177.     // to logical coordinates, such as are stored in the document.
  178.     OnPrepareDC(&dc);  // set up mapping mode and viewport origin
  179.     dc.DPtoLP(&point);
  180.  
  181.     CPen* pOldPen = dc.SelectObject(pDoc->GetCurrentPen());
  182.     dc.MoveTo(m_ptPrev);
  183.     dc.LineTo(point);
  184.     dc.SelectObject(pOldPen);
  185.     m_pStrokeCur->m_pointArray.Add(point);
  186.  
  187.     // Tell the stroke item that we're done adding points to it.
  188.     // This is so it can finish computing its bounding rectangle.
  189.     m_pStrokeCur->FinishStroke();
  190.  
  191.     // Tell the other views that this stroke has been added
  192.     // so that they can invalidate this stroke's area in their
  193.     // client area.
  194.     pDoc->UpdateAllViews(this, 0L, m_pStrokeCur);
  195.  
  196.     ReleaseCapture();   // Release the mouse capture established at
  197.                         // the beginning of the mouse drag.
  198.     return;
  199. }
  200.  
  201. void CScribbleView::OnMouseMove(UINT, CPoint point) 
  202. {
  203.     // Mouse movement is interesting in the Scribble application
  204.     // only if the user is currently drawing a new stroke by dragging
  205.     // the captured mouse.
  206.  
  207.     if (GetCapture() != this)
  208.         return; // If this window (view) didn't capture the mouse,
  209.                 // then the user isn't drawing in this window.
  210.  
  211.     CClientDC dc(this);
  212.     // CScrollView changes the viewport origin and mapping mode.
  213.     // It's necessary to convert the point from device coordinates
  214.     // to logical coordinates, such as are stored in the document.
  215.     OnPrepareDC(&dc);
  216.     dc.DPtoLP(&point);
  217.  
  218.     m_pStrokeCur->m_pointArray.Add(point);
  219.  
  220.     // Draw a line from the previous detected point in the mouse
  221.     // drag to the current point.
  222.     CPen* pOldPen = dc.SelectObject(GetDocument()->GetCurrentPen());
  223.     dc.MoveTo(m_ptPrev);
  224.     dc.LineTo(point);
  225.     dc.SelectObject(pOldPen);
  226.     m_ptPrev = point;
  227.     return;
  228. }
  229.  
  230. void CScribbleView::OnUpdate(CView* /* pSender */, LPARAM /* lHint */, 
  231.     CObject* pHint) 
  232. {
  233.     // The document has informed this view that some data has changed.
  234.  
  235.     if (pHint != NULL)
  236.     {
  237.         if (pHint->IsKindOf(RUNTIME_CLASS(CStroke)))
  238.         {
  239.             // The hint is that a stroke as been added (or changed).
  240.             // So, invalidate its rectangle.
  241.             CStroke* pStroke = (CStroke*)pHint;
  242.             CClientDC dc(this);
  243.             OnPrepareDC(&dc);
  244.             CRect rectInvalid = pStroke->GetBoundingRect();
  245.             dc.LPtoDP(&rectInvalid);
  246.             InvalidateRect(&rectInvalid);
  247.             return;
  248.         }
  249.     }
  250.     // We can't interpret the hint, so assume that anything might
  251.     // have been updated.
  252.     Invalidate(TRUE);
  253.     return;
  254. }
  255.  
  256. void CScribbleView::OnInitialUpdate() 
  257. {
  258.     SetScrollSizes(MM_TEXT, GetDocument()->GetDocSize());
  259.     CScrollView::OnInitialUpdate();
  260. }
  261.